<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      class="query-form"
    >
      <el-form-item label="报警类型编码" prop="lCommand">
        <el-input
          v-model="queryParams.lCommand"
          placeholder="请输入报警类型编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警信息" prop="comment">
        <el-input
          v-model="queryParams.comment"
          placeholder="请输入报警信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="addr">
        <el-input
          v-model="queryParams.addr"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片地址" prop="picUrl">
        <el-input
          v-model="queryParams.picUrl"
          placeholder="请输入图片地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="content-container" style="padding: 0 ;">
      <!-- <div class="handle_bar">
        <div class="tit"><span>报警列表</span></div>
      </div> -->
      <div class="flex-1" style="position: relative;overflow: hidden;">
        <div style="position: absolute;left: 0;top: 0;bottom: 0;right: 0;display: flex;flex-direction: column;overflow: hidden;">
          <el-table
            v-loading="loading"
            :data="warningList"
            class="flex-1"
            height="250"
            style="width:100%;"
          >
            <!-- <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" align="center" prop="id" /> -->
            <el-table-column label="报警类型编码" align="center" prop="lCommand" />
            <el-table-column label="报警信息" align="center" prop="comment" />
            <el-table-column label="地址" align="center" prop="addr" />
            <el-table-column label="图片" align="center" prop="picUrl">
              <template slot-scope="scope">
                <div v-viewer>
                  <img width="60" height="60" :src="scope.row.picUrl">
                </div>
              </template>
            </el-table-column>
            <el-table-column label="地址" align="center" prop="createTime" />
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  v-hasPermi="['bkjg:warning:edit']"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >查看</el-button>
                <!-- <el-button
                  v-hasPermi="['bkjg:warning:remove']"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button> -->
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改摄像机布防警告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="报警类型编码" prop="lCommand">{{ form.lCommand }}
          <!-- <el-input v-model="form.lCommand" placeholder="请输入报警类型编码" /> -->
        </el-form-item>
        <el-form-item label="报警信息" prop="comment">{{ form.comment }}
          <!-- <el-input v-model="form.comment" placeholder="请输入报警信息" /> -->
        </el-form-item>
        <el-form-item label="地址" prop="addr">{{ form.addr }}
          <!-- <el-input v-model="form.addr" placeholder="请输入地址" /> -->
        </el-form-item>
        <el-form-item label="图片" prop="picUrl">
          <div v-viewer>
            <img width="100" height="100" :src="form.picUrl">
          </div>
          <!-- <el-input v-model="form.picUrl" placeholder="请输入图片地址" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarning, getWarning, delWarning, addWarning, updateWarning } from '@/api/fjgs/event/alarm.js'

export default {
  name: 'Warning',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 摄像机布防警告表格数据
      warningList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lCommand: null,
        comment: null,
        addr: null,
        picUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询摄像机布防警告列表 */
    getList() {
      this.loading = true
      listWarning(this.queryParams).then(response => {
        this.warningList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lCommand: null,
        comment: null,
        addr: null,
        createTime: null,
        picUrl: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加摄像机布防警告'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getWarning(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改摄像机布防警告'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWarning(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addWarning(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除摄像机布防警告编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delWarning(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bkjg/warning/export', {
        ...this.queryParams
      }, `bkjg_warning.xlsx`)
    }
  }
}
</script>
